var confirmDialog;
var error = false;
var authenticateDialog;
var confirmWithinDialog;
var isDollarsGlobal;

function checkAnswers(){
	var isSender = document.getElementById("isSender").value;
	var url = 'receiver.php';
	
	if(isSender == 'true')
		url = 'sender.php';
	
	if(error)
		window.location = url;
	
	var question1 = document.getElementsByName("question1");
	if(!question1[0].checked){
		error = true;
		document.getElementById("question1Answer").style.display = "block";
	}
	
	var question2 = document.getElementsByName("question2");
	if(!question2[2].checked){
		error = true;
		document.getElementById("question2Answer").style.display = "block";
	}
	
	var question3 = document.getElementsByName("question3");
	if(!question3[2].checked){
		error = true;
		document.getElementById("question3Answer").style.display = "block";
	}
	
	var question4 = document.getElementsByName("question4");
	if(!question4[3].checked){
		error = true;
		document.getElementById("question4Answer").style.display = "block";
	}
	
	var question5 = document.getElementsByName("question5");
	if(!question5[0].checked){
		error = true;
		document.getElementById("question5Answer").style.display = "block";
	}
	
	var question6 = document.getElementsByName("question6");
	if(!question6[1].checked){
		error = true;
		document.getElementById("question6Answer").style.display = "block";
	}
	
	if(error){
		document.getElementById("answersCheck").style.display = "block";
	}else{
		window.location = url;	
	}	
}

function authenticate(){
    var username=prompt("Enter username");
    var password=prompt("Enter password");
    if(username != "admin" && password != "admin")
            window.location="http://apps.facebook.com/jmj_testa/invalidadmin.php";
}


function gotoRegister(){
	window.location = "register.php"; 
}

function submitSendForm(){
	var sendForm = document.getElementById('sendform');
	confirmDialog.hide();
	sendForm.submit();
}

function submitSendWithinForm(){
	var sendForm = document.getElementById('sendwithinform');
	//confirmWithinDialog.hide();
	sendForm.submit();
}

function showHideRatings(){
	
}

function validateSendForm(isdollars){
	document.getElementById('senderError').style.display = 'block';
	var value = document.getElementById("moneySent").value;
	var currency;
	if(isdollars == 0)
		currency = "&#8360; ";
	else
		currency = "$";
	if(isNaN(value)){
		document.getElementById('senderError').innerHTML = 'Please enter only a number here !';
		document.getElementById('senderError').className = "error";
		return;
	}
	var balance = parseFloat(document.getElementById('balance').value);
	if(parseFloat(value) > balance){
		document.getElementById('senderError').innerHTML = 'You have only ' + currency + balance + ' in your account !';
		document.getElementById('senderError').className = "error";
		return;
	}
	showConfirm();
}

function validateSendWithinForm(isdollars){
    document.getElementById('senderWithinError').style.display = 'block';
	var value1 = document.getElementById("receiverMoney").value;
	var value2 = document.getElementById("receiverMoney1").value;
	var value3 = document.getElementById("receiverMoney2").value;
    var value4 = document.getElementById("receiverMoney3").value;
	var currency;
	if(isdollars == 0)
		currency = "&#8360; ";
	else
		currency = "$";
        
    if(isEmpty(value1) || isEmpty(value2) || isEmpty(value3) || isEmpty(value4)){
		document.getElementById('senderWithinError').innerHTML = 'Please the values to send !';
		document.getElementById('senderWithinError').className = "error";
		return;
	}
	if(isNaN(value1) || isNaN(value2) || isNaN(value3) || isNaN(value4)){
		document.getElementById('senderWithinError').innerHTML = 'Please enter only a number here !';
		document.getElementById('senderWithinError').className = "error";
		return;
	}
	var balance = parseInt(document.getElementById('balance').value);
	if(parseFloat(value1) > balance || parseFloat(value2) > balance || parseFloat(value3) > balance || parseFloat(value4) > balance){
		document.getElementById('senderWithinError').innerHTML = 'You have only ' + currency + balance + ' in your account !';
		document.getElementById('senderWithinError').className = "error";
		return;
	}
	
	var receiverName = document.getElementById("receiverMoneyName").value;
	var receiverName1 = document.getElementById("receiverMoney1Name").value;
	var receiverName2 = document.getElementById("receiverMoney2Name").value;
    var receiverName3 = document.getElementById("receiverMoney2Name").value;
    
    var anonymousIndex = document.getElementById("anonymousIndex").value;
    
    if(anonymousIndex == 0)  receiverName = "Anonymous Receiver";
    else if(anonymousIndex == 1)  receiverName1 = "Anonymous Receiver";
    else if(anonymousIndex == 2)  receiverName2 = "Anonymous Receiver";
    else if(anonymousIndex == 3)  receiverName3 = "Anonymous Receiver";
	
	var message = receiverName + ": " + currency + value1 + '<br/>' + receiverName1 + ": " + currency + value2 + '<br/>' + receiverName2 + ": " + currency + value3 ;
	document.getElementById('receiversConfirm').innerHTML = message;
	submitSendWithinForm();
}

function isDigit( c ){
    return( (c >= "0") && (c <= "9" ) );
}


function isInteger( s ){
    if( isEmpty( s ) ) return false;

    var signCount = 0;
    for( var i = 0; i < s.length; i++ ){
        var c = s.charAt( i );

        if( i == 0 || i == (s.length - 1) )
            if( c == "+" || c == "-" ){
                signCount++;
                continue;
            }

        if( ! isDigit( c ) )
            return false;
    }
    if( signCount > 1 )
        return false;

    return true;
}


function showConfirm(){
	var balance = document.getElementById('moneySent').value;
	document.getElementById("confirmMoney").innerHTML = balance;
	document.getElementById("sendConfirm").style.display = "block";
    confirmDialog = new YAHOO.widget.Dialog("sendConfirm",{ 
        width: "250px", visible:false, modal:true, fixedcenter:true
    });
	confirmDialog.render();
	confirmDialog.show();
}

function showWithinConfirm(){
	document.getElementById("sendWithinConfirm").style.display = "block";
    confirmWithinDialog = new YAHOO.widget.Dialog("sendWithinConfirm",{ 
        width: "250px", visible:false, modal:true, fixedcenter:true
    });
    
    confirmWithinDialog.render();
    confirmWithinDialog.show();
}

function closeWithinConfirm(){
	document.getElementById('senderWithinError').style.display = 'none';
	confirmWithinDialog.hide();
}

function closeConfirm(){
	document.getElementById('senderError').style.display = 'none';
	confirmDialog.hide();
}

function isEmpty( s ){
    s = trim( s );
    return( (s == null) || (s.length == 0) );
}

function isEmailId( s ){
   if( isEmpty( s ) )
       return false;

   return s.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{3,3})$|(\..{2,2}))$)\b/gi);
}

function checkAddress(){
	var address1 = document.getElementById('addr1').value;
	var address2 = document.getElementById('addr2').value;
	var city = document.getElementById('city').value;
	var zip = document.getElementById('zip').value;
	var state = document.getElementById('state').value;
    var country = document.getElementById('country').selectedIndex;
	var zipInt = parseInt(zip);
	
	if(isEmpty(address1))
		return true;
	if(isEmpty(city))
		return true;
	if(isEmpty(zip))
		return true;
	if(isEmpty(state))
		return true;
    if(country == 0)
	    return true;
    return false;
}

function validateRegister(){
	document.getElementById('registerError').style.display="block";
	if(!isEmailId(trim(document.getElementById('email').value))){
		document.getElementById('registerError').innerHTML = "Please enter a valid email address";
		document.getElementById('registerError').className = "error";
		return;
	}
	if(checkAddress()){
		document.getElementById('registerError').innerHTML = "Please check your address";
		document.getElementById('registerError').className = "error";
		return;
	}
	if(checkPayment()){
		document.getElementById('registerError').innerHTML = "Please check your payment options";
		document.getElementById('registerError').className = "error";
		return;
	}
	var frmRegister = document.getElementsByName('frmRegister');
	frmRegister[0].submit();
}

function checkPayment(){
	var radioObj = document.getElementsByName("payment");
	var value = getCheckedValue(radioObj);
	if(value == "check")
		return false;
	else if(value == "paypal"){
		if(trim(document.getElementById("paypalId").value) == "")
			return true;
		else 
			return false;
	}else if (value == "cash"){
		return false;	
	}else if (value == ""){
		return true;
	}	
	return true;
}

function stateChanged(){
	if (xmlhttp.readyState==4){
			alert(xmlhttp.responseText);
	}
}


function trim( s ){
    var i, startPos = 0, endPos = 0;
    for( i = 0; i < s.length; i++ ){
        if( s.charAt(i) != ' ' ){
            startPos = i;
            break;
        }
    }
    for( i = s.length - 1; i >= 0; i-- ){
        if( s.charAt(i) != ' ' ){
            endPos = i + 1;
            break;
        }
    }
	s = s.substring( startPos, endPos );
    return s;
}


function GetXmlHttpObject(){
	if (window.XMLHttpRequest){
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject){
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function paymentChanged(){
	var radioObj = document.getElementsByName("payment");
	var value = getCheckedValue(radioObj);
	if(value == 'paypal')
		document.getElementById('paypalInput').style.display='block';
	else
		document.getElementById('paypalInput').style.display='none';
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
	      
